﻿using CatProxy.Common;
using EpubFactory.Common;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Diagnostics;
using System.Linq;
using System.Linq.Expressions;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace EpubFactory.Hetushu
{
    /// <summary>
    /// SearchWindow.xaml 的交互逻辑
    /// </summary>
    public partial class SearchWindow : Window
    {
        //private SearchViewModel model;
        public SearchWindow()
        {
            InitializeComponent();
            //model = (SearchViewModel)base.DataContext;
        }

        private void btnSearch_Click(object sender, RoutedEventArgs e)
        {
            SearchBooks();
        }

        private void SearchBooks()
        {
            var q = txtQ.Text.Trim();
            if (string.IsNullOrWhiteSpace(q))
            {
                MessageBox.Show("please input keywords");
                return;
            }
            //var t = Task.Factory.StartNew(() =>
            //{
            //    model = _repository.Search(q, this.pageNo);

            //    Dispatcher.Invoke(new Action(() =>
            //    {
            //        lvList.ItemsSource = model.Items;
            //        btnPrev.IsEnabled = model.PageNo > 1;
            //        btnNext.IsEnabled = model.PageNo < model.TotalPages;
            //        lblPage.Content = string.Format("{0}/{1}", model.PageNo, model.TotalPages);
            //    }));
            //});
        }

        private void lvList_MouseDoubleClick(object sender, MouseButtonEventArgs e)
        {
            if (lvList.SelectedIndex > 0)
            {
                var form = new MainWindow("http://www.hetushu.com" + ((HetushuBook)lvList.SelectedItem).SourceUrl);

                form.Show();
            }

        }
    }

    public class SearchViewModel : ObservableObject
    {
        public SearchViewModel()
        {
            this.model = new Pagination<HetushuBook>() { PageNo = 1 };
        }
        private IRepository<HetushuBook> _repository = new HetushuRepository();
        private string q;

        public string Q
        {
            get { return q; }
            set
            {
                q = value;
                RaisePropertyChangedEvent(nameof(Q));
            }
        }
        private bool hasNext;
        public bool HasNext
        {
            get { return hasNext; }
            set
            {
                hasNext = value;
                RaisePropertyChangedEvent(nameof(HasNext));
            }
        }
        private bool hasPrev;

        public bool HasPrev
        {
            get
            {
                return hasPrev;
            }
            set
            {
                hasPrev = value;
                RaisePropertyChangedEvent(nameof(HasPrev));
            }
        }
        public Pagination<HetushuBook> model;
        public Pagination<HetushuBook> Model
        {
            get { return model; }
            set
            {
                model = value;
                RaisePropertyChangedEvent(nameof(Model));
                HasNext = model.PageNo < model.TotalPages;
                HasPrev = model.PageNo > 1;
            }
        }

        public ICommand SearchCommand
        {
            get
            {
                return new DelegateCommand(Search, (s) =>
                {
                    return s != null && !string.IsNullOrWhiteSpace(s.ToString());
                });
            }
        }
        public ICommand NextCommand
        {
            get { return new DelegateCommand(Next); }
        }
        public ICommand PrevCommand
        {
            get { return new DelegateCommand(Prev); }
        }
        private void Search()
        {
            Task.Factory.StartNew(() =>
            {
                this.Model = _repository.Search(this.q, this.Model.PageNo);
            });

        }
        private void Next()
        {
            this.Model.PageNo++;
            Search();
        }
        private void Prev()
        {
            this.Model.PageNo--;
            Search();
        }
    }
}
